<?php
namespace CNEPAdmin\Controller;

use Think\Controller;

abstract class CommonController extends Controller {

    protected $_code = "";
    protected $_error = '';
    protected $_modelName = '';
    protected $_model = null;

    public function _initialize() {
        $ajax = I("ajax");
        /*         * *** 登录验证  start **** */
        $admin = I("session.adminid", null);

        if (intval($admin) <= 0) {
            if ($ajax) { // ajax方式提交
                redirect(U("CNEPAdmin/Login/index"), 3, '{"result":0,"desc":"登录已失效，3秒后将跳转登录页面"}');
            } else {
                $this->error("你还没登陆！~", U("CNEPAdmin/Login/index"));
            }
        }
    }

    /**
     * 检查当前角色对此模块的指定操作是否有指定权限
     * @param string $subCode      子模块
     * @param type $privNo      2：可读写; 1：可读; 0：没权限; 
     */
    public function checkPriv($subCode, $privNo, $byAjaxWs = true) {
        $this->setCode();
        $man_type = $_SESSION['adminArr']['manType'];
        if (intval($man_type) === 1) {
            return;
        }
        $roleID = $_SESSION['adminArr']['roleID'];

        S(C("CACHE_MEM"));
        $priv = S("_" . $roleID . "_" . $this->_code . "_" . $subCode);
        
        if($priv === false || is_null($priv)) {
            D("PrivModule")->sychronRolePriv();
        }
        

//        logErr("key:_" . $roleID . "_" . $this->_code . "_" . $subCode . '======');
//        logErr("_priv:" . $priv . ';privNo:' . $privNo . '=====');
        
//        dump("_priv:" . $priv);
//        dump("_priv:" . $privNo);
//        dump("_" . $roleID . "_" . $this->code . "_" . $subCode);
//        dump($_SESSION);
//        return;

        if($priv == 2) {
            $flag = true;
        } else {
            $flag = (intval($priv) > 0 && intval($priv) === intval($privNo));
        }
        
        if (!$flag) {
            if ($byAjaxWs) { // ajax方式提交
                $this->_error = "无操作权限";
//                dump("_priv:" . $priv);
                $this->_myAjaxReturn(0);
            } else {
                $this->error("不好意思，您无此操作权限~", '', 1);
            }
        }
    }

    protected function _getQueryArr(&$where = array(), &$order = array()) {
        
    }

    /**
     * 设置当前大模块的唯一标识
     */
    protected abstract function setCode();

    /**
     * 查看分页列表,需要设置$this->_modelName为model名称,需要查询条件$where和$order在$this->_getQueryArr中设置就好
     */
    protected function _getOnePageData($listFunc, $lengFunc,$ajax=0, $queryFunc = "_getQueryArr"/*,$other=null*/) {
		/*if($other){
			$where ['og.goodsID'] = array('in',$other);
		}else{
			$where = array();
		}*/
		$where = array();
        $order = array();
        $this->$queryFunc($where, $order);

        $this->_setModel();

        // $count = D("SysAdminRole")->getRoleListLength($where);
        $count = call_user_func_array(array($this->_model, $lengFunc), array($where));
        if ($count > 0) {
            $psize = I("psize", 20);
            $list = call_user_func_array(array($this->_model, $listFunc), array(I("p", 1), $psize, $where, $order));

            // $list = D("SysAdminRole")->getRoleList(I("p", 1), $psize, $where, $order);
            
            $Page = new \Think\Page($count, $psize); // 实例化分页类 传入总记录数和每页显示的记录数
            $Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $show = $Page->show(); // 分页显示输出
            
            $Page->parameter = U( MODULE_NAME . '/' . ACTION_NAME, I("request."));
//            $this->assign('test',  dump($Page));
        }

        $this->assign('lastsql', $this->_model->getLastSql());
		
        if ($ajax) {
        	$this->ajaxReturn(array("list"=>$list,"totalPage"=>$Page->totalPages,"totalRow"=>$Page->totalRows),"json");
        }
        else{
        	$this->assign('list', $list);
        	$this->assign('num', $Page->firstRow); // 赋值分页输出
        	$this->assign('page', $show); // 赋值分页输出
        	$this->display();
        }
    }

    /**
     * 初始化model,需要设置$this->_modelName为model名称
     */
    protected function _setModel() {
        if (is_null($this->_model)) {
            $this->_model = D($this->_modelName);
        }
    }

    /**
     * ajax方式返回
     * @param type $rt
     */
    protected function _myAjaxReturn($rt,$desc='') {
        if (!$rt && empty($this->_error)) {
            $this->_error = '系统繁忙，请稍后重试';
        }
        $rejson = $rt ? array("result" => 1,"desc"=>$rt) : array("result" => 0, "desc" => $this->_error);
        $this->ajaxReturn($rejson);
    }

    /**
     * ajax方式操作(保存、更新、删除等)
     * @param type $funcName model层的操作方法名称
     * @param type $subCode 子模块,若为空则不检查权限
     * @param type $priv 权限(0:无权限;1:可读;2:可写)
     * @param type $data
     */
    protected function _ajaxOperate($funcName, $subCode = '', $priv = '', $data = '') {
        if (!empty($subCode)) {
            $this->checkPriv($subCode, $priv);
        }

        $this->_setModel();
        $rt = $this->_model->$funcName($data);
        
        $this->_error = $this->_model->getError();
        
        $this->_myAjaxReturn($rt);
    }
    
    protected function get_url_path($url){
    	$fUrl=parse_url($url);
    	return $fUrl['path'];
    }

    public function __destruct() {
        //最后做接口日志
    }

    public function tttttt() {
        phpinfo();
    }
    
}

?>